var express = require('express');
var fs   = require("fs");
var util = require('util');//util将任意对象转换为字符串的函数
var querystring = require('querystring');
var Article=require("dbHelp");
// 创建一个渲染器
var router = express.Router();
// 获取HTML布局

/* GET home page. */
router.get('/list2', function(req, res, next) {
	//查找第一条
//	Article.findAll().then(function (datas) {
  var renderer = require('vue-server-renderer').createRenderer()

		var layout = fs.readFileSync('./views/IndustryStandard/vIndustryStandard.html', 'utf8')

var datas=[{'id':'1','author':'张珊','updatedAt':'2017-08-01','title':'你好'},
 			{'id':'2','author':'李思','updatedAt':'2017-08-01','title':'你好'}
 		];
		 renderer.renderToString(
    // 创建一个应用实例
    require('./../views/hybz')(datas),
    // 处理渲染结果
    function (error, html) {
      // 如果渲染时发生了错误
      if (error) {
        // 打印错误到控制台
        console.error(error)
        // 告诉客户端错误
        return res
          .status(500)
          .send('Server Error')
      }
      // 发送布局和HTML文件
      res.send(layout.replace('<div id="app"></div>', html))
    }
  )
//	});
   // 渲染我们的Vue应用为一个字符串
 
});



router.get('/list', function(req,response, next) {
  var renderer = require('vue-server-renderer').createRenderer()

  var layout = fs.readFileSync('./views/IndustryStandard/vIndustryStandard.html', 'utf8')

	var layoutSections = layout.split('<div id="app"></div>');
	var preAppHTML = layoutSections[0];
	var postAppHTML = layoutSections[1];
	var datas=[{'id':'1','author':'张珊','updatedAt':'2017-08-01','title':'你好'},
 			{'id':'2','author':'李思','updatedAt':'2017-08-01','title':'你好'}
 		];
	var stream = renderer.renderToStream(require('./../views/hybz')(datas))
  // 将预先的HTML写入响应
  response.write(preAppHTML)
  // 每当新的块被渲染
  stream.on('data', function (chunk) {
    // 将块写入响应
    response.write(chunk)
  })
  // 当所有的块被渲染完成
  stream.on('end', function () {
    // 将post-app HTML写入响应
    response.end(postAppHTML)
  })
  // 当渲染时发生错误
  stream.on('error', function (error) {
    // 打印错误到控制台
    console.error(error)
    // 告诉客服端发生了错误
    return response
      .status(500)
      .send('Server Error')
  })
});


/* GET home page. */
router.post('/add', function(req, res, next) {
	var _title=req.body.title;
	var _summary=req.body.summary;
	var _content=req.body.content;
  var _source=req.body.source;
    var _imgUrl=req.body.imgUrl;
	var _author=req.body.author;
	var _categoryId='1';

 Article.upsert({
    title: _title,
    summary: _summary,
	content:_content,
	imgUrl:_imgUrl,
	author:_author,
  source:_source,
	categoryId:_categoryId
  }).then(function(test){
	res.send("success");
	});
 
});


/* GET home page. */
router.get('/delete', function(req, res, next) {
	var _id=req.query.id;
	Article.destroy({
	   where:{
		 id:_id
		}
	  }).then(function(user){
		res.send({
                code: 0,
                msg: "删除成功"
            });
	 })
	 
});

/* 获取列表 */
router.get('/getdata', function(req, res, next) {
	//查找第一条
	Article.findAll({where:{categoryId:'1'}}).then(function (articles) {
    var _datas=[];
      for(var i in articles){
        var _obj={
          id: articles[i].dataValues.id,
    title: articles[i].dataValues.title,
    summary:articles[i].dataValues.summary,
	content:articles[i].dataValues.content,
	imgUrl:articles[i].dataValues.imgUrl,
	author:articles[i].dataValues.author,
  source:articles[i].dataValues.source,
	createdAt:'2017-08-11'
  //articles[i].dataValues.createdAt
  }
         _datas.push(_obj);
      }
		  res.send(util.inspect(_datas));

     // res.send(articles);
    })
});


/*获取某一条数据 */
router.get('/getbyid', function(req, res, next) {
    var _id=req.query.id;
	Article.findById(_id).then(function (article) {
       var _obj={
            id: article.dataValues.id,
           title: article.dataValues.title,
           summary:article.dataValues.summary,
           content:article.dataValues.content,
           imgUrl:article.dataValues.imgUrl,
           author:article.dataValues.author,
           source:article.dataValues.source,
           createdAt:'2017-08-11'
       }
        res.send(util.inspect(_obj));
   });

});

  /*修改 */
router.post('/update', function(req, res, next) {
    var _id=req.query.id;
    var _title=req.body.title;
	var _summary=req.body.summary;
	var _content=req.body.content;
    var _imgUrl=req.body.imgUrl;
	var _author=req.body.author;
    var _source=req.body.source;

	//更改
	Article.update({
		title: _title,
		summary: _summary,
		content:_content,
		imgUrl:_imgUrl,
		author:_author,
    source:_source
	  },{where:{
		 id:_id
		}}).then(function (article) {
        res.send({code:0,msg:'修改成功'});
   });
});

module.exports = router;
